package com.bosintek.wordtest.webAccess;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.bosintek.wordtest.WordSet;

public class Loader {
	/**
	 * Loads the words from the default file into the vector of words.
	 * 
	 */
	public Vector<WordSet> loadPlainBank(InputStream plainFormat) {
		Vector<WordSet> allWsets = new Vector<WordSet>();
		String meaning;
		String word;
		try {
			// The default bank is loaded.
			BufferedReader br = new BufferedReader(new InputStreamReader(
					plainFormat, Charset.forName("ISO-8859-1")));

			String line = br.readLine();
			while (line != null) {
				word = line.substring(0, line.indexOf(" - "));
				meaning = line
						.substring(line.indexOf(" - ") + 3, line.length());
				allWsets.add(new WordSet(word, meaning));
				line = br.readLine();
				while (line != null && line.equals("")) {
					line = br.readLine();
				}
			}
			br.close();

		} catch (Exception e) {
			allWsets.add(new WordSet("NO WORDS LEFT", "NO WORDS LEFT"));
			System.out.println("Error de lectura: " + e.getMessage());
		}
		return allWsets;

	}

	/**
	 * It loads words from another file into the words vector.
	 */
	public Vector<WordSet> loadPlainBanks(String filePath) {
		try {
			// TODO: Read file tree and open each file as a different
			// bank.
			return loadPlainBank(new FileInputStream("/sdcard/com.bosintek.wordtest/"
					+ filePath));
		} catch (Exception e) {}
		return new Vector<WordSet>();
	}

	public static String getXML(String user, String pass) {
		String line = null;
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet("http://www.bosintek.com/rest/wtestrest.php?user="+user+"&pass="+pass+"&action=getBanks");
			HttpPost httpPost = new HttpPost("http://p-xr.com/xml");
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			line = EntityUtils.toString(httpEntity);
		} catch (UnsupportedEncodingException e) {
			line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
		} catch (MalformedURLException e) {
			line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
		} catch (IOException e) {
			line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
		}
		return line;
	}
	public Document XMLfromString(String xml){

		Document doc = null;

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        try {

			DocumentBuilder db = dbf.newDocumentBuilder();

			InputSource is = new InputSource();
		        is.setCharacterStream(new StringReader(xml));
		        doc = db.parse(is); 

			} catch (ParserConfigurationException e) {
				System.out.println("XML parse error: " + e.getMessage());
				return null;
			} catch (SAXException e) {
				System.out.println("Wrong XML file structure: " + e.getMessage());
	            return null;
			} catch (IOException e) {
				System.out.println("I/O exeption: " + e.getMessage());
				return null;
			}

	        return doc;

		}
}
